//
//  ZYVoiceButton.m
//  AudientList
//
//  Created by zhangweiwei on 16/6/15.
//  Copyright © 2016年 Erica. All rights reserved.
//

#import "ZYAudioButton.h"
#import "KCCategory.h"

static NSInteger const ZYVoiceImageCount = 3;

@interface ZYAudioButton ()
@property (nonatomic, strong) NSArray *maleImages;
@property (nonatomic, strong) NSArray *femaleImages;
@end

@implementation ZYAudioButton

- (NSArray *)maleImages
{
    if (!_maleImages) {
        
        NSMutableArray *arrayM = @[].mutableCopy;
        for (int i = 1; i <= ZYVoiceImageCount; i++) {
            [arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"voice_male%d", i]]];
            
        }
        _maleImages = arrayM;
    }
    return _maleImages;
}

- (NSArray *)femaleImages
{
    if (!_femaleImages) {
        
        NSMutableArray *arrayM = @[].mutableCopy;
        for (int i = 1; i <= ZYVoiceImageCount; i++) {
            [arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"voice_female%d", i]]];
            
        }
        _femaleImages = arrayM;
    }
    return _femaleImages;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.imageView.contentMode = UIViewContentModeCenter;
        
        self.sex = 0;
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.imageView.kc_x = 0;
    self.imageView.kc_y = 0;
    self.imageView.kc_height = self.kc_height;
    self.imageView.kc_width = self.imageView.kc_height;
    
    self.titleLabel.kc_x = self.imageView.kc_maxX;
    self.titleLabel.kc_y = 0;
    self.titleLabel.kc_height = self.kc_height;
    self.titleLabel.kc_width = self.kc_width - self.titleLabel.kc_x;
}

- (void)setSex:(NSInteger)sex
{
    _sex = sex;
    
    if (sex == 1) {
        
        [self setImage:[UIImage imageNamed:@"voice_female3"] forState:UIControlStateNormal];
        [self setTitleColor:KC_RGB_COLOR(251,117,135) forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:@"round_retangle_female"] forState:UIControlStateNormal];
        
    }else {
        
        
        [self setImage:[UIImage imageNamed:@"voice_male3"] forState:UIControlStateNormal];
        [self setTitleColor:KC_HEX_COLOR(0x67d3ea) forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:@"round_retangle_male"] forState:UIControlStateNormal];
    }
}

- (void)setSecond:(NSString *)second
{
    _second = [second copy];
    
    
    [self setTitle:[NSString stringWithFormat:@"%@\"", second] forState:UIControlStateNormal];
}

- (void)startAnimation
{
    if (self.imageView.isAnimating) return;
    
    self.imageView.animationImages = (self.sex == 1) ? self.femaleImages : self.maleImages;
    self.imageView.animationDuration = ZYVoiceImageCount * 0.5;
    [self.imageView startAnimating];
}

- (void)stopAnimation
{
    
    if (!self.imageView.isAnimating) return;
    
    [self.imageView stopAnimating];
}

- (void)setAnimating:(BOOL)animating
{
    _animating = animating;
    
    if (animating) {
        [self startAnimation];
    }else {
        [self stopAnimation];
    }
}



@end
